public class LinkList {
    class ListNode {
        public int value;//节点的值
        public ListNode next;//下一个节点
        public ListNode(int value) { //实例化节点
            this.value = value;
        }
    }
    //不能放在内部内里面
    public ListNode head;//头节点

    //创建链表
    public void createList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(20);
        ListNode node3 = new ListNode(23);
        ListNode node4 = new ListNode(45);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        this.head = node1;
    }
    //打印链表
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.value + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    //求当前列表节点个数
    public int size() {
        int count = 0;
        ListNode cur = head;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
    //求当前列表是否存在key值
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.value == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //头插法
    public void addFirst(int date) {
        ListNode node = new ListNode(date);
        if(head == null) {
            head = node;
            return;
        }
        //原来的链表连接在node后面
        node.next = head;
        //新的头节点是node
        head = node;
    }
    //尾插法
    public void addLast(int date) {
        ListNode node = new ListNode(date);
        if(head == null) {
            head = node;
            return;
        }
        ListNode cur = head;
        while(cur.next != null) {
            cur = cur.next;
        }
        //cur.next == null
        cur.next = node;
    }
    //中间节点插入
    public void add(int pos,int date) throws indexException {
        if(pos <0 || pos > size()) {
            throw new indexException("index不合法");
        }
        ListNode node = new ListNode(date);
        if(head == null) {
            head = node;
            return;
        }
        if(pos == 0) {
            addFirst(date);
            return;
        }
        if(pos == size()) {
            addLast(date);
            return;
        }
        //中间插入
        //首先查找需要插入位置的前一个节点
        ListNode cur = searchPreIndex(pos);
        node.next = cur.next;
        cur.next = node;
    }
    public ListNode searchPreIndex(int index) {
        ListNode cur = head;
        int count = 0;
        while (count != index-1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }
    //删除第一个数字
    public void remove(int key) {
        if(head == null) {
            return;
        }
        if(head.value == key) {
            head = head.next;
            return;
        }
        ListNode cur = findPreKey(key);
        if(cur == null) {
            return;
        }
        cur.next = cur.next.next;
    }
    //查找关键数字的前一个结点
    private ListNode findPreKey(int key) {
        ListNode cur = head;
        while (cur.next != null) {
            if(cur.value == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    //删除链表包含的所有数字
    public void removeKeyAll(int key) {
        if(head == null) {
            return;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        while (cur != null) {
            if(cur.value == key) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        //除了头节点全部删除了
        if(head.value == key) {
            head = head.next;
        }
    }
}